<template>
  <PageWrapper>
    <n-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="basicTableRef"
        :actionColumn="actionColumn"
        :pagination="false"
        virtual-scroll
	      v-model:expanded-row-keys="expandedKeys"
      >
        <template #tableTitle>
          <n-space align="center">
            <n-button type="primary" @click="openCreateDrawer">
              <template #icon>
                <n-icon>
                  <FileAddOutlined />
                </n-icon>
              </template>
              添加
            </n-button>
          </n-space>
        </template>
      </BasicTable>
    </n-card>

    <CreateDrawer
      ref="createDrawerRef"
      :title="drawerTitle"
      :deptList="tableData"
      @change="reloadTable"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  // import { useMessage } from 'naive-ui';
  import { BasicTable, TableAction, BasicColumn } from '@/components/Table';
  import { TreeList } from '@/api/article/index';
  import { FileAddOutlined } from '@vicons/antd';
  import CreateDrawer from './CreateDrawer.vue';
  import { columns } from './columns';

  // const message = useMessage();
  const basicTableRef = ref();
  const tableData = ref<any[]>([]);
  const createDrawerRef = ref();
  const drawerTitle = ref('添加分类');
  const expandedKeys = ref([]);

  const params = reactive({
    title: '',
  });

  const actionColumn: BasicColumn = reactive({
    width: 150,
    title: '操作',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render(record) {
      return h(TableAction as any, {
        actions: [
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record),
          },
        ],
      });
    },
  });

  const loadDataTable = async (res) => {
    const result = await TreeList({ ...params, ...res });
    tableData.value = result;
    return { list: result };
  };

  function reloadTable() {
    basicTableRef.value.reload();
  }

  function openCreateDrawer() {
    drawerTitle.value = '添加分类';
    const { openDrawer } = createDrawerRef.value;
    openDrawer();
  }

  function handleEdit(record: Recordable) {
    drawerTitle.value = '编辑分类';
    const { openDrawer } = createDrawerRef.value;
    openDrawer(record.id);
  }

</script>

<style lang="less" scoped></style>
